package com.panda.pandawash.util;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.panda.pandawash.R;

public class SelfDialog extends Dialog
{
  private Button yes;
  private Button no;
  private TextView titleTv;
  private TextView messageTv;
  private TextView Unit;
  private RelativeLayout layoutUnit;
  private String titleStr;
  private String messageStr;
  private String valueString;
  private int myVisible = -1;
  private int myLayoutVisible = -1;
  private String yesStr;
  private String noStr;
  private EditText editText;
  private RelativeLayout viewInput;
  private onNoOnclickListener noOnclickListener;
  private onYesOnclickListener yesOnclickListener;
  private String digits;
  private int input_type = -1;
  public static int SELFDIALOG_EDIT_TYPE_EMPTY = -1;
  private String unit;

  public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener)
  {
    if (str != null) {
      this.noStr = str;
    }
    this.noOnclickListener = onNoOnclickListener;
  }

  public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener)
  {
    if (str != null) {
      this.yesStr = str;
    }
    this.yesOnclickListener = onYesOnclickListener;
  }

  public SelfDialog(Context context) {
    super(context, R.style.MyDialog);
  }

  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.free_exercise_sure_dialog_layout);

    setCanceledOnTouchOutside(false);

    initView();

    initData();

    initEvent();
  }

  private void initEvent()
  {
    this.yes.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View v) {
        if (SelfDialog.this.yesOnclickListener != null)
        {
          String editString = null;
          boolean flg = false;

          editString = SelfDialog.this.editText.getText().toString();
          Object times = null;
          if ((String.valueOf(editString) == null) || (editString.equals(""))) {
            times = Integer.valueOf(-1);
          } else {
            times = editString;
            flg = true;
          }
          SelfDialog.this.yesOnclickListener.onYesClick(times, flg);
        }
      }
    });
    this.no.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View v) {
        if (SelfDialog.this.noOnclickListener != null)
          SelfDialog.this.noOnclickListener.onNoClick();
      }
    });
  }

  private void initData()
  {
    if (this.titleStr != null) {
      this.titleTv.setText(this.titleStr);
    }
    if (this.messageStr != null) {
      this.messageTv.setText(this.messageStr);
    }

    if (this.yesStr != null) {
      this.yes.setText(this.yesStr);
    }
    if (this.noStr != null) {
      this.no.setText(this.noStr);
    }
    if (this.myVisible >= 0) {
      this.viewInput.setVisibility(this.myVisible);
    }
    if (this.myLayoutVisible >= 0) {
      this.layoutUnit.setVisibility(this.myLayoutVisible);
    }
    if ((this.digits != null) && (!this.digits.equals(""))) {
      this.editText.setKeyListener(DigitsKeyListener.getInstance(this.digits));
    }
    if (this.input_type != -1) {
      this.editText.setInputType(Integer.valueOf(this.input_type).intValue());
    }
    if (this.unit != null) {
      this.Unit.setText(this.unit);
    }

    if (this.valueString != null) {
      this.editText.setText(this.valueString);
      this.editText.setSelection(this.valueString.length());
    }
  }

  private void initView()
  {
    this.yes = ((Button)findViewById(R.id.yes));
    this.no = ((Button)findViewById(R.id.no));
    this.titleTv = ((TextView)findViewById(R.id.title));
    this.messageTv = ((TextView)findViewById(R.id.message));
    this.editText = ((EditText)findViewById(R.id.edit_time_input));
    this.viewInput = ((RelativeLayout)findViewById(R.id.view_input));
    this.Unit = ((TextView)findViewById(R.id.time_unit));
    this.layoutUnit = ((RelativeLayout)findViewById(R.id.layout_unit));
  }

  public void setTitle(String title)
  {
    this.titleStr = title;
  }

  public void setMessage(String message)
  {
    this.messageStr = message;
  }

  public void setEditVisibility(int visible) {
    this.myVisible = visible;
  }

  public void setEditText(String str) {
    this.valueString = str;
  }

  public void setUnitLayoutVisibility(int myLayoutVisible) {
    this.myLayoutVisible = myLayoutVisible;
  }

  public void setUinit(String unit) {
    this.unit = unit;
  }

  public void setEditDigits(String digits) {
    this.digits = digits;
  }

  public void setEditType(int input_type) {
    this.input_type = input_type;
  }

  public static abstract interface onNoOnclickListener
  {
    public abstract void onNoClick();
  }

  public static abstract interface onYesOnclickListener
  {
    public abstract void onYesClick(Object paramObject, boolean paramBoolean);
  }
}

